import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as menuService from '../../../services/menus'
import * as com from '../../../utils/common';
import COMP_QTZB_DIVI_FHQK from './COMP_QTZB_DIVI_FHQK'
/**同业业务 */
const daoName = "CM_IBANK_CUST_BIZ_SUM_M";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
    let value = ''
    if (num) {
        value = numeral(num).format('0,0')
    }
    return value
};
const columns = [
    { title: '数据日期', width: 130, sorter: true, caption: "SJRQ", key: 'SJRQ', render: data => datetime.format(data.Sjrq, "yyyy-MM-dd"), dispaly: false, filter: true },
    { title: '客户名称', nowrap: true, caption: 'KHMC', key: 'KHMC', width: 260, sorter: true, dispaly: true, filter: true },
    { title: '账户名称', nowrap: true, caption: 'ZHMC', key: 'ZHMC', width: 260, sorter: true, dispaly: true, filter: true },
    { title: '组织机构', dataIndex: 'ZZJG', key: 'ZZJG', width: 130, sorter: true, dispaly: true, filter: true },
    { title: '机构名称', nowrap: true, caption: 'UTNM', key: 'UTNM', width: 120, sorter: true, dispaly: true, filter: true },
    { title: '科目', nowrap: true, caption: 'ITNM', key: 'ITNM', width: 160, sorter: true, dispaly: true, filter: true },
    { title: '起息日', caption: 'Qxr', key: 'Qxr', render: data => datetime.format(data.Qxr, "yyyy-MM-dd"), width: 110, sorter: true, dispaly: true, filter: true },
    { title: '到期日', caption: 'Dqr', key: 'Dqr', render: data => datetime.format(data.Dqr, "yyyy-MM-dd"), width: 110, sorter: true, dispaly: true, filter: true },
    { title: '开户日', caption: 'Khr', key: 'Khr', render: data => datetime.format(data.Khr, "yyyy-MM-dd"), width: 110, sorter: true, dispaly: true, filter: true },
    { title: '销户日', caption: 'Xhr', key: 'Xhr', render: data => datetime.format(data.Xhr, "yyyy-MM-dd"), width: 110, sorter: true, dispaly: true, filter: true },
    { title: '币种', dataIndex: 'CYNM', key: 'CYNM', width: 130, sorter: true, dispaly: true, filter: true },
    { title: '利率', dataIndex: 'INRT', key: 'INRT', width: 130, sorter: true, dispaly: true, filter: true },
    { title: '余额', dataIndex: 'ACBL', key: 'ACBL', width: 130, sorter: true, dispaly: true, filter: true },
    { title: '产品号', dataIndex: 'Cpbh', key: 'Cpbh', width: 130, sorter: true, dispaly: true, filter: true },
    { title: '客户账号', nowrap: true, caption: 'Khzh', key: 'Khzh', width: 180, sorter: true, dispaly: true, filter: true },
    // { title: 'FTP', dataIndex: 'FTP', key: 'FTP', width: 110, sorter: true, dispaly: true, filter: true },
    // { title: '客户经理1', width: 100, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
    // { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB1', render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
    // { title: '客户经理2', width: 100, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
    // { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB2', render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
    // { title: '客户经理3', width: 100, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
    // { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB3', render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
    // { title: '客户经理4', width: 100, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
    // { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB4', render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
    // { title: '客户经理5', width: 100, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
    // { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB5', render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
    { name: 'SJRQ', required: true, type: 'date', label: '数据日期', grid: 12 / 24 },
    { name: 'KHMC', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
    { name: 'ZHMC', required: true, type: 'text', label: '账户名称', grid: 12 / 24 },
    { name: 'ZZJG', required: true, type: 'text', label: '组织机构', grid: 12 / 24 },
    { name: 'UTNM', required: true, type: 'text', label: '机构名称', grid: 12 / 24 },
    { name: 'ITNM', required: true, type: 'text', label: '科目', grid: 12 / 24 },
    { name: 'Qxr', required: true, type: 'date', label: '起息日', grid: 12 / 24 },
    { name: 'Dqr', required: true, type: 'date', label: '到期日', grid: 12 / 24 },
    { name: 'Khr', required: true, type: 'date', label: '开户日', grid: 12 / 24 },
    { name: 'Xhr', required: true, type: 'date', label: '销户日', grid: 12 / 24 },
    { name: 'CYNM', required: true, type: 'text', label: '币种', grid: 12 / 24 },
    { name: 'INRT', required: true, type: 'number', label: '利率', grid: 12 / 24 },
    { name: 'ACBL', required: true, type: 'number', label: '余额', grid: 12 / 24 },
    { name: 'Cpbh', required: true, type: 'text', label: '产品号', grid: 12 / 24 },
    { name: 'Khzh', required: true, type: 'text', label: '客户账号', grid: 12 / 24 },
    { name: 'FTP', required: true, type: 'number', label: 'FTP', grid: 12 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class CM_IBANK_CUST_BIZ_SUM_MForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "STTP",
            Operator: "in",
            FieldValues: '30,31,32'
        })
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks = WhereCauses
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, seeks: seeks }
        });
        this.loadOptionData();
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showModelHandler(isAdd) {
        let key = null
        if (!isAdd) {
            const len = this.props.selectedRowKeys.length;
            if (len === 0) {
                message.info('请选择要认定的数据行！');
                return;
            }
            key = this.props.selectedRowKeys[len - 1]
        }
        this.props.dispatch({
            type: `${daoName}/openEdit`,
            payload: { key }
        });
    }
    findData() {
        const form = this.refs.findForm.getForm();
        let nyc = '';
        let nym = '';
        let isOK = true;
        form.validateFields((err, values) => {
            if (!err) {
                if (values.YF === undefined || values.YF === '') {
                    message.info('请选择！');
                    isOK = false;
                } else {
                    let date = new Date(values.YF.get('year'), values.YF.get('month'), 1, 8, 0, 0);
                    let year = date.getFullYear();
                    let month = date.getMonth() + 1;
                    let day = new Date(year, month, 0);
                    if (month < 10) {
                        month = '0' + month;
                    }
                    nym = year + '-' + month + '-' + day.getDate();
                    nyc = datetime.format(date, "yyyy-MM-dd");
                }
            }
        });
        if (!isOK) {
            return;
        }
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "Sjrq",
            Operator: ">=",
            FieldValues: nyc
        }, {
                FieldName: "Sjrq",
                Operator: "<=",
                FieldValues: nym
            });
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks = WhereCauses;
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, seeks }
        });
    }
    saveData() {
        const len = this.props.selectedRowKeys.length;
        if (len === 0) {
            message.info('请选择要编辑的行，如果选择了多行，则使用最后一次选择！');
            return;
        }
        let ZSFL = 0;
        const form = this.refs.findForm.getForm();
        form.validateFields((err, values) => {
            if (!err) {
                if (values.ZSFL === undefined || values.ZSFL === '') {
                    message.info('请输入FTP值！');
                    isOK = false;
                } else {
                    ZSFL = Number(values.ZSFL);
                }
            }
        });
        const NO = this.props.selectedRowKeys[0];
        this.props.dispatch({
            type: `${daoName}/UpdateTyywFtp`,
            payload: { daoName, NO, ZSFL }
        });
    }
    SelDivi(value) {
        let whereItems = [];
        let WhereCauses = [];
        let isdivi = ''
        if (value === 'all') {
            isdivi = '30,31,32'
        } else {
            isdivi = value
        }
        whereItems.push({
            FieldName: "STTP",
            Operator: "in",
            FieldValues: isdivi
        })
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks = WhereCauses
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, seeks }
        });
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        let pTypes = popedomTypes[permID]
        let data = []
        const controlsFilter = [{ name: 'ISDIVI', required: false, type: 'select', label: '选择', grid: 4 / 24, data: [{ value: 'all', label: '全部' }, { value: '30', label: '同业负债' }, { value: '31', label: '同业资产' }, { value: '32', label: '同业投资' }], onChange: this.SelDivi.bind(this), layout: { labelSpan: 8, wrapperSpan: 13 } }]
        if (pTypes.indexOf("2") !== -1) {
            data.push({ value: '1', label: '修改FTP', onClick: this.saveData.bind(this) })
            controlsFilter.push({ name: 'ZSFL', required: false, type: 'text', label: 'FTP', grid: 7 / 24, layout: { labelSpan: 4, wrapperSpan: 20 } })
        }
        if (pTypes.indexOf("6") !== -1) {
            data.push({ value: '2', label: '业绩认定', onClick: this.showModelHandler.bind(this, false) })
        }
        if (data.length > 0) {
            controlsFilter.push({ name: 'CX', required: false, type: 'button-group', data: data, grid: 7 / 24 })
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} record={{ ISDIVI: 'all' }} clearItemMargin={true} />
                    </div>
                    <COMP_QTZB_DIVI_FHQK ref="divi" jlid={record.ID} ywid={13} title={"【" + record.KHMC + "】 业绩分配"} showEdit={showEdit} parentModel={daoName} />
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                </div>
            </div>
        )
    }
}
function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state.CM_IBANK_CUST_BIZ_SUM_M;
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record
    }
}
CM_IBANK_CUST_BIZ_SUM_MForm = connect(mapStateToProps)(CM_IBANK_CUST_BIZ_SUM_MForm)
export default CM_IBANK_CUST_BIZ_SUM_MForm;
let render = (props) => React.createElement(CM_IBANK_CUST_BIZ_SUM_MForm, props);

let f = new Factory()
f.set("CM_IBANK_CUST_BIZ_SUM_MForm", render)


